﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RPSImporter.Infrastructure.Attributes;
using RPSImporter.Infrastructure;
using RPSImporter.Services.Data;
using RPSImporter.Models.Settings;
using RPSImporter.Services;

namespace RPSImporter.Models
{
   [FileTypeName("Companies_FileType")]
   public class Company : ModelBase, IModel
   {
      public Company()
      {
         this.FileSettings = DatabaseSvc.Instance.Settings.CompanyFileSettings.First();
      }

      public CompanyFileSettings FileSettings { get; set; }

      public bool Import()
      {
         try
         {
            if (this.GetData(this.FileSettings.Name) != String.Empty
               && !this.DB.COMPANies.Any(c => c.NAME == this.GetData(this.FileSettings.Name)))
            {
               COMPANY company = new COMPANY();
               company.NAME = this.GetData(this.FileSettings.Name);
               company.ADDRESS1 = this.GetData(this.FileSettings.Address1);
               company.ADDRESS2 = this.GetData(this.FileSettings.Address2);
               company.CH_CASE_POSTALE = this.GetData(this.FileSettings.ChCasePostale);
               company.CITY = (from c in this.DB.CITies
                               where c.NAME == this.GetData(this.FileSettings.City)
                               select c).FirstOrDefault();
               company.COUNTRY = (from c in this.DB.COUNTRies
                                  where c.NAME == this.GetData(this.FileSettings.Country)
                                  select c).FirstOrDefault();
               company.EMAIL = this.GetData(this.FileSettings.Email);
               company.FAX = this.GetData(this.FileSettings.Fax);
               company.PHONE = this.GetData(this.FileSettings.Phone);
               company.POSTAL_BOX = this.GetData(this.FileSettings.PostalBox);
               company.POSTAL_CODE = this.GetData(this.FileSettings.PostalCode);
               company.US_ZIPCODE = this.GetData(this.FileSettings.USZipCode);
               company.WEBSITE = this.GetData(this.FileSettings.Website);

               this.DB.COMPANies.InsertOnSubmit(company);
               this.DB.SubmitChanges();
            }
            else
            {
               COMPANY company = this.DB.COMPANies.FirstOrDefault(c => c.NAME == this.GetData(this.FileSettings.Name));
               company.ADDRESS1 = this.GetData(this.FileSettings.Address1);
               company.ADDRESS2 = this.GetData(this.FileSettings.Address2);
               company.CH_CASE_POSTALE = this.GetData(this.FileSettings.ChCasePostale);
               company.CITY = (from c in this.DB.CITies
                               where c.NAME == this.GetData(this.FileSettings.City)
                               select c).FirstOrDefault();
               company.COUNTRY = (from c in this.DB.COUNTRies
                                  where c.NAME == this.GetData(this.FileSettings.Country)
                                  select c).FirstOrDefault();
               company.EMAIL = this.GetData(this.FileSettings.Email);
               company.FAX = this.GetData(this.FileSettings.Fax);
               company.NAME = this.GetData(this.FileSettings.Name);
               company.PHONE = this.GetData(this.FileSettings.Phone);
               company.POSTAL_BOX = this.GetData(this.FileSettings.PostalBox);
               company.POSTAL_CODE = this.GetData(this.FileSettings.PostalCode);
               company.US_ZIPCODE = this.GetData(this.FileSettings.USZipCode);
               company.WEBSITE = this.GetData(this.FileSettings.Website);
               this.DB.SubmitChanges();
            }
            return true;
         }
         catch (Exception ex)
         {
            this.Error = ex.Message;
            return false;
         }
      }
   }
}
